package com.learn.jdk;

import java.util.Objects;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * @author: lipan
 * @date: 2021/5/16 10:43
 */

public  class spliteratorDemo {





    String [] data;


   public spliteratorDemo(String text){
       this.data=text.split("\n");
   }


    public static void main(String[] args) {

        IntStream intStream = IntStream.rangeClosed(0, 10);
          //打印乱序
//        intStream.parallel().forEach(System.out::println);


//        Spliterator.OfInt spliterator = intStream.spliterator();
//
//        Consumer<Integer> consumer=i-> System.out.println(i);
//
//        spliterator.forEachRemaining(consumer);



         String text="在向你挥舞的各色花帕中\n" +
                "\n" +
                "是谁的手突然收回\n" +
                "\n" +
                "紧紧捂住了自己的眼睛\n" +
                "\n" +
                "当人们四散离去，谁\n" +
                "\n" +
                "还站在船尾\n" +
                "\n" +
                "衣裙漫飞，如翻涌不息的云　　　　　　　　\n" +
                "\n" +
                "江涛\n" +
                "\n" +
                "　　高一声\n" +
                "\n" +
                "　　　　　低一声\n" +
                "\n" +
                "\n" +
                "\n" +
                "美丽的梦留下美丽的优伤\n" +
                "\n" +
                "人间天上，代代相传\n" +
                "\n" +
                "但是，心\n" +
                "\n" +
                "真能变成石头吗\n" +
                "\n" +
                "为盼望远天的杳鹤\n" +
                "\n" +
                "而错过无数次春江月明\n" +
                "\n" +
                "沿着江岸\n" +
                "\n" +
                "金光菊和女贞子的洪流\n" +
                "\n" +
                "正煽动新的背叛\n" +
                "\n" +
                "与其在悬崖上展览千年\n" +
                "\n" +
                "不如在爱人肩头痛哭一晚";


         //非并行
//        StreamSupport.stream(new MySpliterator(text),false).forEach(System.out::println);

        //并行
        StreamSupport.stream(new MySpliterator(text),true).forEach(System.out::println);


    }

}

class  MySpliterator implements Spliterator<String>{


    private int start,end;

    private static String[] data;

    public MySpliterator(String text) {
        Objects.requireNonNull(text,"入参不能为空");
        this.start = 0;
        data=text.split("\n");
        this.end =data.length-1;
        System.out.println("aaaaa");
    }

    public MySpliterator(int start, int end) {
        this.start = start;
        this.end = end;
    }

    /**
     * 判断是否有元素可以消费
     * @param action
     * @return
     */
    @Override
    public boolean tryAdvance(Consumer<? super String> action) {

        if(end>=start){
            action.accept(data[start++]);
            return true;
        }
        return false;
    }

    /**
     *
     * 对数据做切割
     * @return
     */
    @Override
    public Spliterator<String> trySplit() {
        //此时无法再分
        if(start>=end){
            return null;
        }

        int left=start;
        int right=(start+end)/2;
        start=right+1;
        return new MySpliterator(left,right);
    }

    @Override
    public long estimateSize() {
        return end-start;
    }

    @Override
    public long getExactSizeIfKnown() {
        return estimateSize();
    }

    @Override
    public int characteristics() {
        return IMMUTABLE |SIZED | SUBSIZED ;
    }
}
